/*******************************************************************************
 *
 * File Name:          ForumDAO.java
 *
 * Created:            2006/10/29 08:58:09
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2006/10/29 08:58:09 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/
package org.njo.webapp.root.model.dao;

/*******************/
/* IMPORT SECTION  */
/*******************/

import org.njo.webapp.root.utility.ObjectNotExistsException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;


/**
 * 数据访问对象.<br>
 * 访问数据库中表数据.
 * 提供以下几种方法:
 * <OL>
 * <LI><B>insert</B> 向数据库表中插入一条记录.</LI>
 * <LI><B>update</B> 更新数据库表中某条记录.</LI>
 * <LI><B>delete</B> 删除数据库表中指定主键的数据.</LI>
 * <LI><B>deleteAllRecords</B> 删除数据库表中所有的数据.</LI>
 * <LI><B>select</B> 从数据库表中取得指定主键的数据.</LI>
 * <LI><B>selectAllRecords(int,int)</B> 从数据库表中取得指定索引范围的所有数据.</LI>
 * <LI><B>selectAllRecords</B> 从数据库表中取得所有数据.</LI>
 * <LI><B>size</B> 从数据库表中取得所有数据的条数.</LI>
 * </OL>
 * <p/>
 * <pre>
 * 使用方法:
 *     Connection connection = null;
 *     ForumDAO forumDAO = null;
 * <p/>
 *     // open the database connection.
 *     try {
 *         connection = SystemContext.getDatabaseConnection();
 *     } catch (Throwable tw) {
 *         logger.error(tw);
 *         throw new ActivityError(tw);
 *     }
 * <p/>
 *     try {
 *         // access the database.
 *         // call the DAO Object insert the record into DB.
 *         forumDAO = new ForumDAO(connection);
 *         count = forumDAO.insert( new Forum());
 * <p/>
 *         connection.commit();
 *     } catch (SQLException ex) {
 *         // when fail rollback the updated's data.
 *         try {
 *             connection.rollback();
 *         } catch (Throwable tw) {
 *             // when rollback fail,log the info.
 *             logger.error(tw);
 *         }
 *         logger.error(ex);
 *         throw new ActivityError(ex);
 *     } finally {
 *         try {
 *             if (connection != null) {
 *                 SystemContext.closeConnection(connection);
 *             }
 *         } catch (Throwable tw) {
 *             //
 *             logger.error(tw);
 *             throw new ActivityError(tw);
 *         }
 *     }
 * 注意:
 *     数据库连接对象(java.sql.Connection)的实例应由调用者创建,提供并操作,
 *     在本对象中不对数据库连接对象(java.sql.Connection)的实例进行任何操作,
 *     在本对象中不对任何的SQL异常处理,需要由调用者处理相应的SQL异常.
 * </pre>
 *
 * @author yu.peng
 * @version 1.00
 */
public class ForumDAO {

    /**
     * 数据库连接对象.
     */
    private Connection connection = null;

    /**
     * 构造函数.<br>
     * 需要一个数据库连接对象作为参数.
     *
     * @param argConnection 数据库连接对象
     */
    public ForumDAO(Connection argConnection) {
        connection = argConnection;
    }

    /**
     * 向数据库表中插入一条记录.<br>
     * 将argForum中的数据插入到数据库中.
     * <p/>
     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
     * 修改,请删除这段TODO注释.
     *
     * @param id
     * @param title
     * @param content
     * @param attachment
     * @param whenreplytomail
     * @param flg
     * @param replyId
     * @param board
     * @param author
     * @param host
     * @return
     * @throws SQLException
     */
    public int insert(
            String id,
            String title,
            String content,
            String attachment,
            int whenreplytomail,
            int flg,
            String replyId,
            String board,
            String previousid,
            String nextid,
            String samesubjectpreviousid,
            String author,
            String host) throws SQLException {
        StringBuffer insertSql = null;
        PreparedStatement insertStatement = null;
        try {
            insertSql = new StringBuffer();
            insertSql.append(" INSERT INTO FORUM ( ");
            insertSql.append(" ID , ");
            insertSql.append(" TITLE , ");
            insertSql.append(" CONTENT , ");
            insertSql.append(" ATTACHMENT , ");
            insertSql.append(" WHENREPLYTOMAIL , ");
            insertSql.append(" FLG , ");
            insertSql.append(" REPLYID , ");
            insertSql.append(" BOARD , ");
            insertSql.append(" PREVIOUSID , ");
            insertSql.append(" NEXTID , ");
            insertSql.append(" SAMESUBJECTPREVIOUSID , ");
            insertSql.append(" AUTHOR , ");
            insertSql.append(" HOST , ");
            insertSql.append(" CREATETIMESTAMP , ");
            insertSql.append(" UPDATETIMESTAMP ) ");
            insertSql.append(" VALUES ( ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" ? , ");
            insertSql.append(" NOW() , ");
            insertSql.append(" NOW() ) ");

            insertStatement = connection.prepareStatement(insertSql.toString());
            insertStatement.setString(1, id);
            insertStatement.setString(2, title);
            insertStatement.setString(3, content);
            insertStatement.setString(4, attachment);
            insertStatement.setInt(5, whenreplytomail);
            insertStatement.setInt(6, flg);
            insertStatement.setString(7, replyId);
            insertStatement.setString(8, board);
            insertStatement.setString(9, previousid);
            insertStatement.setString(10, nextid);
            insertStatement.setString(11, samesubjectpreviousid);
            insertStatement.setString(12, author);
            insertStatement.setString(13, host);

            return insertStatement.executeUpdate();
        } finally {
            if (insertStatement != null) {
                insertStatement.close();
                insertStatement = null;
            }
        }
    }

    public int updateNextId(String id, String nextid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" NEXTID = ? ");
            updateSql.append(" WHERE ID = ? ");

            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, nextid);
            updateStatement.setString(2, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }

    public int updatePreviousId(String id , String previousid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" PREVIOUSID = ? ");
            updateSql.append(" WHERE ID = ? ");

            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, previousid);
            updateStatement.setString(2, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }

    public int updateSNextId(String id, String snextid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" SAMESUBJECTNEXTID = ? ");
            updateSql.append(" WHERE ID = ? ");
            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, snextid);
            updateStatement.setString(2, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }

    public int deleteSubject(String id) throws SQLException {
        StringBuffer deleteSql = null;
        PreparedStatement deleteStatement = null;
        try {
            deleteSql = new StringBuffer();
            deleteSql.append(" UPDATE FORUM SET ");
            deleteSql.append(" FLG = -1 ");
            deleteSql.append(" WHERE ID = ? ");

            deleteStatement = connection.prepareStatement(deleteSql.toString());

            deleteStatement.setString(1, id);

            return deleteStatement.executeUpdate();
        } finally {
            if (deleteStatement != null) {
                deleteStatement.close();
                deleteStatement = null;
            }
        }
    }

    public int takeTop(String id, String previousid, String nextid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" FLG = 100, ");
            updateSql.append(" PREVIOUSID = ? , ");
            updateSql.append(" NEXTID = ? , ");
            updateSql.append(" UPDATETIMESTAMP = NOW() ");
            updateSql.append(" WHERE ID = ? ");

            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, previousid);
            updateStatement.setString(2, nextid);
            updateStatement.setString(3, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }

    public int unTop(String id, String previousid,  String nextid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" FLG = 10, ");
            updateSql.append(" PREVIOUSID = ? , ");
            updateSql.append(" NEXTID = ? , ");
            updateSql.append(" UPDATETIMESTAMP = NOW() ");
            updateSql.append(" WHERE ID = ? ");

            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, previousid);
            updateStatement.setString(2, nextid);
            updateStatement.setString(3, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }


    public int updateSPreviousId(String id , String spreviousid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" SAMESUBJECTPREVIOUSID = ? ");
            updateSql.append(" WHERE ID = ? ");

            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, spreviousid);
            updateStatement.setString(2, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }

    public int updateSameSubjectNextId(String id, String samesubjectnextid) throws SQLException {
        StringBuffer updateSql = null;
        PreparedStatement updateStatement = null;
        try {
            updateSql = new StringBuffer();
            updateSql.append(" UPDATE FORUM SET ");
            updateSql.append(" SAMESUBJECTNEXTID = ? ");
            updateSql.append(" WHERE ID = ? ");

            updateStatement = connection.prepareStatement(updateSql.toString());

            updateStatement.setString(1, samesubjectnextid);
            updateStatement.setString(2, id);

            return updateStatement.executeUpdate();
        } finally {
            if (updateStatement != null) {
                updateStatement.close();
                updateStatement = null;
            }
        }
    }

    public List getReplys(String id) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        Map forum = null;
        List resultList = new ArrayList();
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID , ");
            selectSql.append("        PREVIOUSID , ");
            selectSql.append("        NEXTID , ");
            selectSql.append("        SAMESUBJECTPREVIOUSID , ");
            selectSql.append("        SAMESUBJECTNEXTID  ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE REPLYID = ? ");
            selectSql.append(" AND FLG <> -1 ");
            selectSql.append(" ORDER BY SEQNO DESC ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, id);

            resultSet = selectStatement.executeQuery();

            while (resultSet.next()) {
                forum = new HashMap();
                forum.put("id", resultSet.getString(1));
                forum.put("previousid", resultSet.getString(2));
                forum.put("nextid", resultSet.getString(3));
                forum.put("samesubjectpreviousid", resultSet.getString(4));
                forum.put("samesubjectnextid", resultSet.getString(5));

                resultList.add(forum);
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return resultList;
    }

    /**
     * 从数据库表中取得指定索引范围的所有数据.<br>
     * 从数据库表中取得从argStartIndex到argEndIndex的数据,并使用数据对象封装,顺序排列
     * 在结果列表中.
     * <p/>
     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
     * 修改,请删除这段TODO注释.
     *
     * @param argStartIndex 开始索引
     * @param argEndIndex   结束索引
     * @return 取得的数据列表
     * @throws SQLException SQL异常
     */
    public List selectSubjectsByBoard(String board,
                                      int argStartIndex,
                                      int argEndIndex) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        Map forum = null;
        List resultList = new ArrayList();
        int curIndex = 0;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID , ");
            selectSql.append("        SEQNO, ");
            selectSql.append("        TITLE , ");
            selectSql.append("        ATTACHMENT , ");
            selectSql.append("        FLG , ");
            selectSql.append("        AUTHOR , ");
            selectSql.append("        UPDATETIMESTAMP ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE BOARD = ? ");
            selectSql.append(" AND FLG <> -1 ");
            selectSql.append(" ORDER BY FLG DESC, UPDATETIMESTAMP DESC, SEQNO DESC ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, board);

            resultSet = selectStatement.executeQuery();
            if (resultSet.absolute(argStartIndex)) {
                curIndex = argStartIndex;
                do {
                    forum = new HashMap();
                    forum.put("id", resultSet.getString(1));
                    forum.put("seqno", resultSet.getString(2));
                    forum.put("title", resultSet.getString(3));
                    forum.put("attachment", resultSet.getString(4));
                    forum.put("flg", resultSet.getString(5));
                    forum.put("author", resultSet.getString(6));
                    forum.put("updatetimestamp", resultSet.getTimestamp(7));

                    resultList.add(forum);
                    curIndex++;
                } while (resultSet.next() && (curIndex <= argEndIndex));
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return resultList;
    }

    /**
     * 从数据库表中取得指定索引范围的所有数据.<br>
     * 从数据库表中取得从argStartIndex到argEndIndex的数据,并使用数据对象封装,顺序排列
     * 在结果列表中.
     * <p/>
     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
     * 修改,请删除这段TODO注释.
     *
     * @param argStartIndex 开始索引
     * @param argEndIndex   结束索引
     * @return 取得的数据列表
     * @throws SQLException SQL异常
     */
    public String getLastReplyNextId(String replyid) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        String nextId = null;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ");
            selectSql.append("        NEXTID ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE REPLYID = ? ");
            selectSql.append(" AND FLG <> -1 ");
            selectSql.append(" ORDER BY SEQNO DESC ");
            selectSql.append(" LIMIT 1 ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, replyid);

            resultSet = selectStatement.executeQuery();
            if (resultSet.next()) {
                nextId = resultSet.getString(1);
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return nextId;
    }

    public int sizeOfBoard(String board) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT COUNT(*) AS CNT FROM FORUM ");
            selectSql.append(" WHERE BOARD = ? ");
            selectSql.append(" AND FLG <> -1 ");

            selectStatement = connection.prepareStatement(selectSql.toString());

            selectStatement.setString(1, board);

            resultSet = selectStatement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
    }

    /**
     * 从数据库表中取得所有数据的条数.<br>
     * 从数据库表中取得所有数据的条数,并返回.
     * <p/>
     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
     * 修改,请删除这段TODO注释.
     *
     * @return 所有数据的条数
     * @throws SQLException SQL异常
     */
    public int replyCountById(String id) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        int count = 0;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT COUNT(*) AS CNT ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE REPLYID = ? ");
            selectSql.append(" AND FLG <> -1 ");

            selectStatement = connection.prepareStatement(selectSql.toString());

            selectStatement.setString(1, id);
            resultSet = selectStatement.executeQuery();
            if (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
    }

    public String getLastIdWithTop(String board) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        String id = null;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE BOARD  = ? ");
            selectSql.append(" AND FLG <> -1 ");
            selectSql.append(" ORDER BY FLG DESC, UPDATETIMESTAMP DESC, SEQNO DESC ");
            selectSql.append(" LIMIT 1 ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, board);

            resultSet = selectStatement.executeQuery();

            if (resultSet.next()) {
                id = resultSet.getString(1);
            } else {
                return null;
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return id;
    }

    public String getLastId(String board) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        String id = null;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE BOARD  = ? ");
            selectSql.append(" AND FLG = 10 ");
            selectSql.append(" ORDER BY UPDATETIMESTAMP DESC, SEQNO DESC ");
            selectSql.append(" LIMIT 1 ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, board);

            resultSet = selectStatement.executeQuery();

            if (resultSet.next()) {
                id = resultSet.getString(1);
            } else {
                return null;
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return id;
    }

    public String getFirstIdOfTop(String board) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        String id = null;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE BOARD  = ? ");
            selectSql.append(" AND FLG = 100 ");
            selectSql.append(" ORDER BY UPDATETIMESTAMP, SEQNO ");
            selectSql.append(" LIMIT 1 ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, board);

            resultSet = selectStatement.executeQuery();

            if (resultSet.next()) {
                id = resultSet.getString(1);
            } else {
                return null;
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return id;
    }

    public String getSameSubjectPreviousId(String board, String replyid) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        String id = null;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE BOARD  = ? ");
            selectSql.append("   AND REPLYID  = ? ");
            selectSql.append("   AND FLG <> -1 ");
            selectSql.append(" ORDER BY SEQNO DESC ");
            selectSql.append(" LIMIT 1 ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, board);
            selectStatement.setString(2, replyid);

            resultSet = selectStatement.executeQuery();

            if (resultSet.next()) {
                id = resultSet.getString(1);
            } else {
                return null;
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return id;
    }

    /**
     * TODO.<br>
     *
     * @param id 主题id
     * @return 取得的数据列表
     * @throws SQLException SQL异常
     */
    public Map selectSubject(String id) throws SQLException, ObjectNotExistsException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        Map forum = null;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ID , ");
            selectSql.append("        TITLE , ");
            selectSql.append("        CONTENT , ");
            selectSql.append("        ATTACHMENT , ");
            selectSql.append("        WHENREPLYTOMAIL , ");
            selectSql.append("        FLG , ");
            selectSql.append("        REPLYID , ");
            selectSql.append("        BOARD , ");
            selectSql.append("        PREVIOUSID , ");
            selectSql.append("        NEXTID , ");
            selectSql.append("        SAMESUBJECTPREVIOUSID , ");
            selectSql.append("        SAMESUBJECTNEXTID , ");
            selectSql.append("        AUTHOR , ");
            selectSql.append("        HOST , ");
            selectSql.append("        CREATETIMESTAMP , ");
            selectSql.append("        UPDATETIMESTAMP ");
            selectSql.append(" FROM FORUM ");
            selectSql.append(" WHERE ID = ? ");
            selectSql.append(" AND FLG <> -1 ");

            selectStatement = connection.prepareStatement(
                    selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, id);

            resultSet = selectStatement.executeQuery();

            if (resultSet.next()) {
                forum = new HashMap();
                forum.put("id", resultSet.getString(1));
                forum.put("title", resultSet.getString(2));
                forum.put("content", resultSet.getString(3));
                forum.put("attachment", resultSet.getString(4));
                forum.put("whenreplytomail", resultSet.getString(5));
                forum.put("flg", resultSet.getString(6));
                forum.put("replyid", resultSet.getString(7));
                forum.put("board", resultSet.getString(8));

                forum.put("previousid", resultSet.getString(9));
                forum.put("nextid", resultSet.getString(10));
                forum.put("samesubjectpreviousid", resultSet.getString(11));
                forum.put("samesubjectnextid", resultSet.getString(12));
                forum.put("author", resultSet.getString(13));
                forum.put("host", resultSet.getString(14));
                forum.put("createtimestamp", resultSet.getTimestamp(15));
                forum.put("updatetimestamp", resultSet.getTimestamp(16));
            } else {
                throw new ObjectNotExistsException("not found data in database.");
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return forum;
    }



//    /**
//     * 向数据库表中插入一条记录.<br>
//     * 将argForum中的数据插入到数据库中.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * @param argForum 将要插入的数据
//     * @return 被插入数据的条数
//     * @throws SQLException SQL异常
//     */
//    public int insert( Forum argForum ) throws SQLException {
//        StringBuffer insertSql = null;
//        PreparedStatement insertStatement = null;
//        try {
//            insertSql = new StringBuffer();
//            insertSql.append(" INSERT INTO forum ( ");
//            insertSql.append(" ID , ");
//            insertSql.append(" TITLE , ");
//            insertSql.append(" CONTENT , ");
//            insertSql.append(" ATTACHMENT , ");
//            insertSql.append(" WHENREPLYTOMAIL , ");
//            insertSql.append(" FLG , ");
//            insertSql.append(" REPLYID , ");
//            insertSql.append(" BOARDID , ");
//            insertSql.append(" AUTHOR , ");
//            insertSql.append(" HOST , ");
//            insertSql.append(" CREATETIMESTAMP , ");
//            insertSql.append(" UPDATETIMESTAMP ) ");
//            insertSql.append(" VALUES ( ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? , ");
//            insertSql.append(" ? ) ");
//
//            insertStatement = connection.prepareStatement ( insertSql.toString () );
//            insertStatement.setString ( 1, argForum.getId () );
//            insertStatement.setString ( 2, argForum.getTitle () );
//            insertStatement.setString ( 3, argForum.getContent () );
//            insertStatement.setString ( 4, argForum.getAttachment () );
//            insertStatement.setInt ( 5, argForum.getWhenreplytomail () );
//            insertStatement.setInt ( 6, argForum.getFlg () );
//            insertStatement.setString ( 7, argForum.getReplyid () );
//            insertStatement.setString ( 8, argForum.getBoardid () );
//            insertStatement.setString ( 9, argForum.getAuthor () );
//            insertStatement.setString ( 10, argForum.getHost () );
//            insertStatement.setTimestamp ( 11, argForum.getCreatetimestamp ()==null ? null : new java.sql.Timestamp(argForum.getCreatetimestamp().getTime()) );
//            insertStatement.setTimestamp ( 12, argForum.getUpdatetimestamp ()==null ? null : new java.sql.Timestamp(argForum.getUpdatetimestamp().getTime()) );
//
//            return insertStatement.executeUpdate ();
//        } finally {
//            if (insertStatement!=null) {
//                insertStatement.close ();
//                insertStatement = null;
//            }
//        }
//    }
//
//    /**
//     * 更新数据库表中某条记录.<br>
//     * 根据argForum中的主键,更新数据库中的数据,但是主键的字段不会被更新.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * @param argForum 将要更新的数据
//     * @return updated 被更新数据的条数
//     * @throws SQLException SQL异常
//     */
//    public int update( Forum argForum ) throws SQLException {
//        StringBuffer updateSql = null;
//        PreparedStatement updateStatement = null;
//        try {
//            updateSql = new StringBuffer();
//            updateSql.append(" UPDATE forum SET ");
//            updateSql.append(" ID = ? , ");
//            updateSql.append(" TITLE = ? , ");
//            updateSql.append(" CONTENT = ? , ");
//            updateSql.append(" ATTACHMENT = ? , ");
//            updateSql.append(" WHENREPLYTOMAIL = ? , ");
//            updateSql.append(" FLG = ? , ");
//            updateSql.append(" REPLYID = ? , ");
//            updateSql.append(" BOARDID = ? , ");
//            updateSql.append(" AUTHOR = ? , ");
//            updateSql.append(" HOST = ? , ");
//            updateSql.append(" CREATETIMESTAMP = ? , ");
//            updateSql.append(" UPDATETIMESTAMP = ? ");
//            updateSql.append(" WHERE ID = ? ");
//
//
//            updateStatement = connection.prepareStatement ( updateSql.toString () );
//
//            updateStatement.setString ( 1, argForum.getId () );
//            updateStatement.setString ( 2, argForum.getTitle () );
//            updateStatement.setString ( 3, argForum.getContent () );
//            updateStatement.setString ( 4, argForum.getAttachment () );
//            updateStatement.setInt ( 5, argForum.getWhenreplytomail () );
//            updateStatement.setInt ( 6, argForum.getFlg () );
//            updateStatement.setString ( 7, argForum.getReplyid () );
//            updateStatement.setString ( 8, argForum.getBoardid () );
//            updateStatement.setString ( 9, argForum.getAuthor () );
//            updateStatement.setString ( 10, argForum.getHost () );
//            updateStatement.setTimestamp ( 11, argForum.getCreatetimestamp ()==null ? null : new java.sql.Timestamp(argForum.getCreatetimestamp().getTime()) );
//            updateStatement.setTimestamp ( 12, argForum.getUpdatetimestamp ()==null ? null : new java.sql.Timestamp(argForum.getUpdatetimestamp().getTime()) );
//
//            updateStatement.setString ( 13, argForum.getId () );
//
//
//            return updateStatement.executeUpdate ();
//        } finally {
//            if ( updateStatement != null ) {
//                updateStatement.close ();
//                updateStatement = null;
//            }
//        }
//    }
//
//    /**
//     * 删除数据库表中指定主键的数据.<br>
//     * 根据主键,删除数据库中的数据.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * @param argId String
//     * @return 被删除数据的条数
//     * @throws SQLException SQL异常
//     */
//    public int delete( String argId ) throws SQLException {
////    public int delete(  ) throws SQLException {
//        StringBuffer deleteSql = null;
//        PreparedStatement deleteStatement = null;
//        try {
//            deleteSql = new StringBuffer ();
//            deleteSql.append(" DELETE FROM forum ");
//            deleteSql.append(" WHERE ID = ? ");
//
//
//            deleteStatement = connection.prepareStatement ( deleteSql.toString () );
//
//            deleteStatement.setString ( 1, argId );
//
//
//            return deleteStatement.executeUpdate ();
//        } finally {
//            if ( deleteStatement != null ) {
//                deleteStatement.close ();
//                deleteStatement = null;
//            }
//        }
//    }
//
//    /**
//     * 删除数据库表中所有的数据.<br>
//     *
//     * @return 被删除数据的条数
//     * @throws SQLException SQL异常
//     */
//    public int deleteAllRecords() throws SQLException {
//        StringBuffer deleteSql = null;
//        PreparedStatement deleteStatement = null;
//        try {
//            deleteSql = new StringBuffer ();
//            deleteSql.append(" DELETE FROM forum ");
//
//            deleteStatement = connection.prepareStatement ( deleteSql.toString () );
//            return deleteStatement.executeUpdate ();
//        } finally {
//            if ( deleteStatement != null ) {
//                deleteStatement.close ();
//                deleteStatement = null;
//            }
//        }
//    }
//
//    /**
//     * 从数据库表中取得指定主键的数据.<br>
//     * 根据主键,从数据库中取得数据.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * <pre>
//     * 注意:如果指定的条件没有取得到数据时,将会抛出一个RecordNotFoundException异常,
//     *      应该在调用这个方法的地方处理这个异常.
//     * </pre>
//     *
//     * @param argId String
//     * @return 取得的数据对象实例
//     * @throws SQLException SQL异常
//     * @throws RecordNotFoundException 没有找到数据
//     */
//    public Forum select( String argId ) throws SQLException, RecordNotFoundException {
////    public Forum select(  ) throws SQLException, RecordNotFoundException {
//
//        StringBuffer selectSql = null;
//        PreparedStatement selectStatement = null;
//        ResultSet resultSet = null;
//        Forum forum = null;
//        try {
//            selectSql = new StringBuffer ();
//            selectSql.append(" SELECT ID , ");
//            selectSql.append("        TITLE , ");
//            selectSql.append("        CONTENT , ");
//            selectSql.append("        ATTACHMENT , ");
//            selectSql.append("        WHENREPLYTOMAIL , ");
//            selectSql.append("        FLG , ");
//            selectSql.append("        REPLYID , ");
//            selectSql.append("        BOARDID , ");
//            selectSql.append("        AUTHOR , ");
//            selectSql.append("        HOST , ");
//            selectSql.append("        CREATETIMESTAMP , ");
//            selectSql.append("        UPDATETIMESTAMP ");
//            selectSql.append(" FROM forum ");
//            selectSql.append(" WHERE ID = ? ");
//
//
//            selectStatement = connection.prepareStatement (
//                    selectSql.toString (),
//                    ResultSet.TYPE_SCROLL_INSENSITIVE,
//                    ResultSet.CONCUR_READ_ONLY);
//
//            selectStatement.setString ( 1, argId );
//
//
//            resultSet = selectStatement.executeQuery ();
//
//            if (resultSet.next ()) {
//                forum = new Forum ();
//                forum.setId( resultSet.getString ( 1 ) );
//                forum.setTitle( resultSet.getString ( 2 ) );
//                forum.setContent( resultSet.getString ( 3 ) );
//                forum.setAttachment( resultSet.getString ( 4 ) );
//                forum.setWhenreplytomail( resultSet.getInt ( 5 ) );
//                forum.setFlg( resultSet.getInt ( 6 ) );
//                forum.setReplyid( resultSet.getString ( 7 ) );
//                forum.setBoardid( resultSet.getString ( 8 ) );
//                forum.setAuthor( resultSet.getString ( 9 ) );
//                forum.setHost( resultSet.getString ( 10 ) );
//                forum.setCreatetimestamp( resultSet.getTimestamp ( 11 ) );
//                forum.setUpdatetimestamp( resultSet.getTimestamp ( 12 ) );
//
//            } else {
//                throw new RecordNotFoundException("not found data in database.");
//            }
//        } finally {
//            if ( resultSet != null ) {
//                resultSet.close ();
//                resultSet = null;
//            }
//            if ( selectStatement != null ) {
//                selectStatement.close ();
//                selectStatement = null;
//            }
//        }
//        return forum;
//    }
//
//    /**
//     * 从数据库表中取得指定索引范围的所有数据.<br>
//     * 从数据库表中取得从argStartIndex到argEndIndex的数据,并使用数据对象封装,顺序排列
//     * 在结果列表中.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * @param argStartIndex 开始索引
//     * @param argEndIndex 结束索引
//     * @return 取得的数据列表
//     * @throws SQLException SQL异常
//     */
//    public List selectAllRecords( int argStartIndex, int argEndIndex ) throws SQLException {
//        StringBuffer selectSql = null;
//        PreparedStatement selectStatement = null;
//        ResultSet resultSet = null;
//        Forum forum = null;
//        List resultList = new ArrayList ();
//        int curIndex = 0;
//        try {
//            selectSql = new StringBuffer ();
//            selectSql.append(" SELECT ID , ");
//            selectSql.append("        TITLE , ");
//            selectSql.append("        CONTENT , ");
//            selectSql.append("        ATTACHMENT , ");
//            selectSql.append("        WHENREPLYTOMAIL , ");
//            selectSql.append("        FLG , ");
//            selectSql.append("        REPLYID , ");
//            selectSql.append("        BOARDID , ");
//            selectSql.append("        AUTHOR , ");
//            selectSql.append("        HOST , ");
//            selectSql.append("        CREATETIMESTAMP , ");
//            selectSql.append("        UPDATETIMESTAMP ");
//            selectSql.append(" FROM forum ");
//
//            selectStatement = connection.prepareStatement (
//                    selectSql.toString (),
//                    ResultSet.TYPE_SCROLL_INSENSITIVE,
//                    ResultSet.CONCUR_READ_ONLY );
//
//            resultSet = selectStatement.executeQuery ();
//            if ( resultSet.absolute ( argStartIndex ) ) {
//                curIndex = argStartIndex;
//                do {
//                    forum = new Forum ();
//                    forum.setId( resultSet.getString ( 1 ) );
//                    forum.setTitle( resultSet.getString ( 2 ) );
//                    forum.setContent( resultSet.getString ( 3 ) );
//                    forum.setAttachment( resultSet.getString ( 4 ) );
//                    forum.setWhenreplytomail( resultSet.getInt ( 5 ) );
//                    forum.setFlg( resultSet.getInt ( 6 ) );
//                    forum.setReplyid( resultSet.getString ( 7 ) );
//                    forum.setBoardid( resultSet.getString ( 8 ) );
//                    forum.setAuthor( resultSet.getString ( 9 ) );
//                    forum.setHost( resultSet.getString ( 10 ) );
//                    forum.setCreatetimestamp( resultSet.getTimestamp ( 11 ) );
//                    forum.setUpdatetimestamp( resultSet.getTimestamp ( 12 ) );
//
//                    resultList.add ( forum );
//                    curIndex++;
//                } while ( resultSet.next () && ( curIndex <= argEndIndex ) );
//            }
//        } finally {
//            if ( resultSet != null ) {
//                resultSet.close ();
//                resultSet = null;
//            }
//            if ( selectStatement != null ) {
//                selectStatement.close ();
//                selectStatement = null;
//            }
//        }
//        return resultList;
//    }
//    /**
//     * 从数据库表中取得所有数据.<br>
//     * 从数据库表中取得所有数据,并使用数据对象封装,顺序排列在结果列表中.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * @return 取得的数据列表
//     * @throws SQLException SQL异常
//     */
//    public List selectAllRecords() throws SQLException {
//        return selectAllRecords ( 1, Integer.MAX_VALUE );
//    }
//    /**
//     * 从数据库表中取得所有数据的条数.<br>
//     * 从数据库表中取得所有数据的条数,并返回.
//     *
//     * TODO:根据具体的需要可能需要一些修改,如果已经修改了,请在这里加上注释,如果不需要
//     *      修改,请删除这段TODO注释.
//     *
//     * @return 所有数据的条数
//     * @throws SQLException SQL异常
//     */
//    public int size() throws SQLException {
//        StringBuffer selectSql = null;
//        PreparedStatement selectStatement = null;
//        ResultSet resultSet = null;
//        int count = 0;
//        try {
//            selectSql = new StringBuffer ();
//            selectSql.append(" SELECT COUNT(*) AS CNT FROM forum ");
//
//            selectStatement = connection.prepareStatement ( selectSql.toString () );
//            resultSet = selectStatement.executeQuery ();
//            if ( resultSet.next () ) {
//                count = resultSet.getInt ( 1 );
//            }
//            return count;
//        } finally {
//            if ( resultSet != null ) {
//                resultSet.close ();
//                resultSet = null;
//            }
//            if ( selectStatement != null ) {
//                selectStatement.close ();
//                selectStatement = null;
//            }
//        }
//    }
//


}
